package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.SouYiDetailBean
import com.example.wallet.db.UserInfoManager
import com.example.wallet.global.AppGlobals
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type
import java.text.DecimalFormat

/**
 * 收益明细
 * */
class SouYiDetailFragment : SimpleListFragment<SouYiDetailBean>() {

    companion object {
        fun newInstance(): SouYiDetailFragment {
            val args = Bundle()
            val fragment = SouYiDetailFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private val iconType = AppGlobals.getApplication().getString(R.string.wallet_cml)

    private var dfs: DecimalFormat? = DecimalFormat("0.000000")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isEnableLoadMore = false
        isAddDividerItemDecoration = false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        mHeader.setBackgroundResource(R.color.bg_color_0)
        addHeader(R.layout.comment_header_layout)

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "收益明细"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mRecyclerView.setPadding(0, PixUtils.dp2px(14), 0, 0)
        mRecyclerView.clipToPadding = false
    }

    override fun getItemLayout(): Int {
        return R.layout.sou_yi_detail_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: SouYiDetailBean?) {
        helper?.setText(R.id.time, item?.createTime)
            ?.setText(R.id.icon, "${item?.txNums.toString()} $iconType")
            ?.setText(R.id.name, "${item?.name}")
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(SouYiDetailBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.temp_fack_data)
//        val jsonFromRaw =""
//            parseDate(jsonFromRaw)

        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val map = mapOf(
            "pageCount" to Int.MAX_VALUE,
            "pageIndex" to pageIndex,
            "uid" to userInfo?.uid
        )
        RestClient.builder()
            .url(R.string.queryBalanceLogList)
            .raw(JSON.toJSONString(map))
            .success {
                parseDate(it)
            }
            .failure {
                onFailed()
            }.error { _, _ ->
                onFailed()
            }.build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val list = obj["list"]
        val array = JSON.parseArray(list.toString(), SouYiDetailBean::class.java)
        loadListDate(array)
    }
}